<?php
/**
 * 提现账单控制器
 * @文件名称: TestController.php
 * @author: jawei
 * @Date: 2017-06-06
 * @Copyright: 2017 悦畅科技有限公司. All rights reserved.
 * 注意：本内容仅限于悦畅科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */

namespace app\components;

use app\base\BaseException;

class DmpCurl
{
    public static function post($url, $content)
    {
        return self::basePost($url, $content, true);
    }

    public static function fastPost($url, $content)
    {
        return self::basePost($url, $content, false);
    }

    protected static function basePost($url, $content, $needLog)
    {
        $options = [
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER  => false,
            CURLOPT_CONNECTTIMEOUT => 50, // timeout on connect
            CURLOPT_TIMEOUT => 100, // timeout on response
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $content,
            CURLOPT_CUSTOMREQUEST => 'POST',
        ];

        $ch = curl_init($url);
        if ($ch) {
            curl_setopt_array($ch, $options);
            $data = curl_exec($ch);
            curl_close($ch);
            if($needLog){
                $logInfo = [
                    'url' => $url,
                    'params' => $content,
                    'result' => $data
                ];
                DmpLog::log($logInfo, false);
            }
        } else {
            throw new BaseException('curl post fail: ' . curl_error($ch), 50031);
        }
        return $data;
    }
}
